<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('设备库存管理')"/>
    <link rel="stylesheet" th:href="@{/admin/css/other/user.css}"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" lay-filter="device-query-form">
            <div class="layui-form-item">
                <label class="layui-form-label">设备名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="deviceName" placeholder="" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>

<div class="user-left " style="height: 450px;" id="device_sort">
    <div class="layui-card">
        <div class="layui-card-body">
            <div id="device_sort_3" class="button button-default user-group" user-group="3"> 全部设备</div>
            <div id="device_sort_0" class="button button-default user-group" user-group="0"> 办公类设备</div>
            <div id="device_sort_1" class="button button-default user-group" user-group="1"> 科研类设备</div>
            <div id="device_sort_2" class="button button-default user-group" user-group="2"> 其他设备</div>
        </div>
    </div>
</div>

<div class="user-main">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="user-table" lay-filter="user-table"></table>
        </div>
    </div>
</div>

</body>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增入库
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        报废
    </button>
</script>


<script type="text/html" id="user-bar">
    {{#if (d.scrap == 0) { }}
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
    {{# } }}
</script>

<script type="text/html" id="user-enable">
    <input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用"
           lay-filter="user-enable" {{d.userLock=='0' ? 'checked' : '' }}>
</script>

<script type="text/html" id="requisition-enable">
    {{#if (d.requisition == 0) { }}
    <a class="pear-btn pear-btn-primary pear-btn-sm" style="cursor:default"> 未领用 </a>
    {{# }else if(d.requisition == 1){ }}
    <a class="pear-btn pear-btn-danger pear-btn-sm" style="cursor:default">
        已领用
    </a>
    {{# } }}
</script>

<script type="text/html" id="repair-enable">
    {{#if (d.repair == 0) { }}
    <a class="pear-btn pear-btn-primary pear-btn-sm" style="cursor:default"> 正常 </a>
    {{# }else if(d.repair == 1){ }}
    <a class="pear-btn pear-btn-warming pear-btn-sm" style="cursor:default">
        维修
    </a>
    {{# } }}
</script>


<script type="text/html" id="scrap-enable">
    {{#if (d.scrap == 0) { }}
    <a class="pear-btn pear-btn-primary pear-btn-sm" style="cursor:default"> 正常 </a>
    {{# }else if(d.scrap == 1){ }}
    <a class="pear-btn pear-btn-danger pear-btn-sm" style="cursor:default">
        报废
    </a>
    {{# } }}
</script>


<th:block th:include="include :: footer"/>
<script>
    layui.use(['table', 'form', 'jquery', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        let MODULE_PATH = "/device/";

        let cols = [
            [
                {type: 'checkbox'},
                {title: '设备名', field: 'deviceName', width: 250},
                {title: '制造公司', field: 'deviceCompany', width: 150},
                {title: '单价', field: 'devicePrice', width: 100},
                {title: '入库时间', field: 'storageTime', width: 170},

                {title: '是否领用', field: 'requisition', templet: '#requisition-enable', width: 110},
                {title: '领用人', field: 'requisitionName', width: 100},
                {title: '领用时间', field: 'requisitionTime', width: 170},
                {title: '归还时间', field: 'returnTime', width: 170},

                {title: '是否维修', field: 'repair', templet: '#repair-enable', width: 110},
                {title: '维修时间', field: 'repairTime', width: 170},
                {title: '是否报废', field: 'scrap', templet: '#scrap-enable', width: 110},
                {title: '报废时间', field: 'scrapTime', width: 170},
                {title: '备注', field: 'remark', width: 200},
                {title: '操作', toolbar: '#user-bar', align: 'center', width: 130}
            ]
        ]

        table.render({
            elem: '#user-table',
            url: MODULE_PATH + 'data',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('tool(user-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(user-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });


        $(".user-group").click(function () {
            let group = $(this).attr("user-group");
            let field = form.val("device-query-form");
            if (group === "0") {
                field.sort = 0;
                $('#device_sort_0').removeClass("button-default");
                $('#device_sort_0').addClass("button-primary");
                $('#device_sort_1').removeClass("button-primary");
                $('#device_sort_1').addClass("button-default");
                $('#device_sort_2').removeClass("button-primary");
                $('#device_sort_2').addClass("button-default");
                $('#device_sort_3').removeClass("button-primary");
                $('#device_sort_3').addClass("button-default");
            } else if (group === "1") {
                field.sort = 1;
                $('#device_sort_1').removeClass("button-default");
                $('#device_sort_1').addClass("button-primary");
                $('#device_sort_0').removeClass("button-primary");
                $('#device_sort_0').addClass("button-default");
                $('#device_sort_2').removeClass("button-primary");
                $('#device_sort_2').addClass("button-default");
                $('#device_sort_3').removeClass("button-primary");
                $('#device_sort_3').addClass("button-default");
            } else if (group === "2") {
                field.sort = 2;
                $('#device_sort_2').removeClass("button-default");
                $('#device_sort_2').addClass("button-primary");
                $('#device_sort_0').removeClass("button-primary");
                $('#device_sort_0').addClass("button-default");
                $('#device_sort_1').removeClass("button-primary");
                $('#device_sort_1').addClass("button-default");
                $('#device_sort_3').removeClass("button-primary");
                $('#device_sort_3').addClass("button-default");
            } else if (group === "3") {
                field.sort = '';
                $('#device_sort_3').removeClass("button-default");
                $('#device_sort_3').addClass("button-primary");
                $('#device_sort_0').removeClass("button-primary");
                $('#device_sort_0').addClass("button-default");
                $('#device_sort_1').removeClass("button-primary");
                $('#device_sort_1').addClass("button-default");
                $('#device_sort_2').removeClass("button-primary");
                $('#device_sort_2').addClass("button-default");
            }
            window.refresh(field);
        })


        window.add = function () {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['450px', '500px'],
                content: MODULE_PATH + 'add'
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['450px', '500px'],
                content: MODULE_PATH + 'edit?id=' + obj.data['id']
            });
        }

        window.remove = function (obj) {
            layer.confirm('确定要报废该设备', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('user-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            var checkIds = common.checkField(obj, 'id');
            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要报废这些设备', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "batchRemove/" + checkIds,
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('user-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        form.on('submit(user-query)', function (data) {
            table.reload('user-table', {
                where: data.field
            })
            return false;
        });

        form.on('submit(user-query)', function (data) {
            window.refresh(data.field);
            return false;
        });
        window.refresh = function (param) {
            table.reload('user-table', {where: param});
        }
    })
</script>
</html>